package cn.blnp.net.flowable.boot.config.flowable.listener;

import cn.blnp.net.flowable.boot.service.impl.IdRouterService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.stereotype.Component;

/**
 * <h3>全局任务创建监听器</h3>
 *
 * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
 * @version 1.0
 * @since 2025/7/2 16:57
 */
@Slf4j
@Component
@AllArgsConstructor
public class GlobalTaskCreateListener extends AbstractFlowableEngineEventListener {

    private final IdRouterService idRouterService;

    @Override
    protected void taskCreated(FlowableEngineEntityEvent event) {
        TaskEntity taskEntity = (TaskEntity) (event).getEntity();
        //将任务ID路由信息写入路由表
        idRouterService.addTaskId(taskEntity.getId());
    }

    @Override
    public boolean isFailOnException() {
        //表示一旦流程实例ID路由信息写入失败，则回滚整个事务，任务创建失败。
        return true;
    }
}
